package com.lxb.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxb.commonutils.JwtUtils;
import com.lxb.commonutils.R;
import com.lxb.eduservice.client.OrderClient;
import com.lxb.eduservice.dto.CourseFrontDto;
import com.lxb.eduservice.dto.CourseInfoFormDto;
import com.lxb.eduservice.entity.EduCourse;
import com.lxb.eduservice.entity.EduTeacher;
import com.lxb.eduservice.service.EduChapterService;
import com.lxb.eduservice.service.EduCourseService;
import com.lxb.eduservice.service.EduSubjectService;
import com.lxb.eduservice.service.EduTeacherService;
import com.lxb.eduservice.vo.chapter.ChapterVo;
import com.lxb.eduservice.vo.front.CourseWebVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 前台，讲师接口
 *
 * @author xuebin.li
 * @createTime 2023/1/3 13:16
 */
@Api(description = "前台:课程接口")
@RestController
@RequestMapping("/eduservice/coursefront")
//@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    @ApiOperation("多条件分页查询课程")
    @PostMapping("/getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit, @RequestBody CourseFrontDto courseFrontDto) {

        Page<EduCourse> coursePage = new Page<>(page, limit);

        //调用分页方法
        Map<String, Object> map = courseService.pageCourse(coursePage, courseFrontDto);

        //返回分页数据
        return R.ok().data(map);
    }

    @ApiOperation("根据课程id，查询课程信息以及章节小节(课程大纲)")
    @GetMapping("/getFromCourseInfo/{courseId}")
    public R getFromCourseInfo(@PathVariable String courseId, HttpServletRequest request) {

        //1.查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);

        //2.根据课程id，查询课程 章节和小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);

        //3.远程调用订单模块，判断用户是否购买该课程
        boolean isbuy = orderClient.isByCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));

        if (courseWebVo != null) {
            //增加课程浏览次数，每调用一次就加一
            EduCourse course = new EduCourse();
            BeanUtils.copyProperties(courseWebVo, course);

            course.setViewCount((long) (courseWebVo.getViewCount() + 1.0));
            System.out.println("增加-》》》》》》" + (courseWebVo.getViewCount() + 1.0));
            //增加课程浏览数
            courseService.updateById(course);

        }
        return R.ok().data("courseWebVo", courseWebVo).data("chapterVideoList", chapterVideoList).data("isbuy", isbuy);
    }

    /**
     * 记录课程购买次数
     *
     * @param courseId
     * @return
     */
    @GetMapping("/addBuyCount/{courseId}")
    public boolean addBuyCount(@PathVariable("courseId") String courseId) {

        EduCourse eduCourse = courseService.getById(courseId);

        UpdateWrapper<EduCourse> update = new UpdateWrapper<>();
        update.set("buy_count",(long) (eduCourse.getBuyCount() + 1.0));//set实现的是sql语句的update set
        update.eq("id",courseId);//where部分

        boolean b = courseService.update(null, update);

        return b;
    }
}
